package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.vo.Category2Vo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> treeList() {
        //1.查询所有的数据
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //2.从所有的数据中找一级分类
        List<CategoryEntity> l1List = allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).map(l1 -> {
            List<CategoryEntity> l2List = getChildren(l1, allList);
            l1.setChildren(l2List);
            return l1;
        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return l1List;
    }



    private List<CategoryEntity> getChildren(CategoryEntity item,List<CategoryEntity> allList){
        List<CategoryEntity> l2List = allList.stream().filter(category -> {
            return category.getParentCid().equals(item.getCatId());
        }).map(c -> {
            List<CategoryEntity> children = getChildren(c, allList);
            c.setChildren(children);
            return c;
        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return l2List;
    }

   /* @Override
    public List<CategoryEntity> treeList() {
        //1.查询所有的一级分类
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_cid",0);
        List<CategoryEntity> l1List = baseMapper.selectList(wrapper);
        //2.遍历一级分类
        for (CategoryEntity l1 : l1List) {
            //通过一级分类找二级分类
            Long catId = l1.getCatId();
            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
            wrapper.eq("parent_cid",catId);
            List<CategoryEntity> l2List = baseMapper.selectList(w2);
            //4.把查询到的二级分类赋值给一级分类
            l1.setChildren(l2List);
            //5.遍历二级分类找三级分类
            for (CategoryEntity l2 : l2List) {
                QueryWrapper<CategoryEntity> w3 = new QueryWrapper<>();
                w3.eq("parent_cid",l2.getCatId());
                List<CategoryEntity> l3List = baseMapper.selectList(w3);
                l2.setChildren(l3List);

            }


        }


        return l1List;
    }*/


    @Override
    public List<CategoryEntity> getL1List() {
        //1.查询所有的一级分类
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_cid",0);
        List<CategoryEntity> l1List = baseMapper.selectList(wrapper);




        return l1List;
    }

    @Override
    public Map<Long, List<Category2Vo>> findCatelog() {
        //1.查询所有的数据
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //2.从所有的数据中找一级分类
        Map<Long, List<Category2Vo>> map=  allList.stream().filter(item -> {
            return item.getParentCid().equals(0L);
        }).map(l1->{
            return l1;
        }).collect(Collectors.toMap(k->{
            return k.getCatId();
        },v->{
            List<Category2Vo> l2list = allList.stream().filter(item -> {
                return item.getParentCid().equals(v.getCatId());
            }).map(l2 -> {
                Category2Vo category2Vo = new Category2Vo();
                category2Vo.setCatalog1Id(v.getCatId());
                category2Vo.setName(l2.getName());
                category2Vo.setId(l2.getCatId());
                List<Category2Vo.Category3Vo> l3list = allList.stream().filter(item -> {
                    return item.getParentCid().equals(l2.getCatId());
                }).map(l3 -> {
                    Category2Vo.Category3Vo category3Vo = new Category2Vo.Category3Vo();
                    category3Vo.setCatalog2Id(l2.getCatId());
                    category3Vo.setId(l3.getCatId());
                    category3Vo.setName(l3.getName());
                    return category3Vo;
                }).collect(Collectors.toList());
                category2Vo.setCatalog3List(l3list);

                return category2Vo;
            }).collect(Collectors.toList());

            return l2list;
        }));

        return map;
    }

}